const SIGN_REGEXP = /([yMdhsm])(\1*)/g
const DEFAULT_PATTERN = 'yyyy-MM-dd'

function padding (s, len) {
  const length = len - (s + '').length
  for (let i = 0; i < length; i++) {
    s = '0' + s
  }
  return s
}

export default {
  formatDate: {
    format: function (date, pattern) {
      const patternNew = pattern || DEFAULT_PATTERN
      return patternNew.replace(SIGN_REGEXP, function ($0) {
        switch ($0.charAt(0)) {
          case 'y':
            return padding(date.getFullYear(), $0.length)
          case 'M':
            return padding(date.getMonth() + 1, $0.length)
          case 'd':
            return padding(date.getDate(), $0.length)
          case 'w':
            return date.getDay() + 1
          case 'h':
            return padding(date.getHours(), $0.length)
          case 'm':
            return padding(date.getMinutes(), $0.length)
          case 's':
            return padding(date.getSeconds(), $0.length)
        }
      })
    },
  }
}


/**
 * 字符转换
 * @param {*} text
 */
 export function charPattern(text) {
   if(text !== null){
     text = text.replace('&#65288;', '');
     text = text.replace('&#65289;', '');
   }
    return text;
 }

 /**
 * 逗号转换顿号
 * @param {*} text
 */
 export function charSymbol(text) {
   if(text !== null){
     text = text.replaceAll(',', '、');
   }
    return text;
 }

 /**
  * 中文转换
  * @param {*} text
  */
  export function chinaShow(text) {
     return text? "是" : "否";
  }

  /**
  * 专职兼职转换
  * @param {*} text
  */
  export function isPartTimeShow(text) {
    return text ? "专职" : "兼职";
  }

/**
 * 支部书记还是党委书记
 * @param {*} text
 */
export function zbsjOrDwsjShow(text) {
  return endWith(text) ? "党委书记" : "支部书记";
}

function endWith(text) {
  let str = '公司委员会';
  const legnth = text.length - str.length;
  return (legnth >= 0 && str.lastIndexOf(text) == legnth)
}

/**
 * 判空
 * @param {*} object
 */
export function isEmpty(object) {
  return (object === undefined || object === null || object.length <= 0);
}

/**
 * 非空判断
 * @param {*} object
 */
export function isNotEmpty(object) {
  return (object !== undefined && object !== null && object.length > 0);
}

/**
 * 职务（党内职务、行政职务）.
 * @param partyPost
 * @param administrativeDuties
 * @returns {*}
 */
export function postsShow(partyPost, administrativeDuties) {
  if (partyPost === administrativeDuties) {
    return partyPost
  }
  const arr = [partyPost, administrativeDuties];
  let retStr = "";
  for (const item of arr) {
    if (item !== null) {
      retStr += "、" + item;
    }
  }
  return retStr.slice(1);
}
